home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_014 / termcap / tgetstr.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  279 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *            Copyright (c) 1982, Fred Fish            *
  4.  *                All Rights Reserved                *
  5.  *                                    *
  6.  *    This software and/or documentation is released for public    *
  7.  *    distribution for personal, non-commercial use only.        *
  8.  *    Limited rights to use, modify, and redistribute are hereby    *
  9.  *    granted for non-commercial purposes, provided that all        *
  10.  *    copyright notices remain intact and all changes are clearly    *
  11.  *    documented.  The author makes no warranty of any kind with    *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular    *
  14.  *    purpose.                            *
  15.  *                                    *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20. /*
  21.  *  LIBRARY FUNCTION
  22.  *
  23.  *    tgetstr   extract string capability from termcap entry
  24.  *
  25.  *  KEY WORDS
  26.  *
  27.  *    termcap
  28.  *
  29.  *  SYNOPSIS
  30.  *
  31.  *    char *tgetstr(id,area)
  32.  *    char *id;
  33.  *    char **area;
  34.  *
  35.  *  DESCRIPTION
  36.  *
  37.  *    Gets the string capability for <id>, placing it in
  38.  *    the buffer at *area, and advancing *area to point
  39.  *    to next available storage.
  40.  *
  41.  *    For example, if the following capabilities are
  42.  *    in the termcap file:
  43.  *
  44.  *        ZZ=zzzz
  45.  *        YY=yyyyyy
  46.  *        WW=www
  47.  *
  48.  *    then successive calls using YY, ZZ, and WW will
  49.  *    build the following buffer:
  50.  *
  51.  *        yyyyyy0zzzz0www0
  52.  *
  53.  *    The first call will return a pointer to yyyyyy, the
  54.  *    second will return a pointer to zzzz and the third
  55.  *    will return a pointer to www.  Note that each
  56.  *    string is null terminated, as are all C strings.
  57.  *
  58.  *    Characters preceded by the carot character (\136)
  59.  *    are mapped into the corresponding control character.
  60.  *    For example, the two character sequence ^A becomes
  61.  *    a single control-A (\001) character.
  62.  *
  63.  *    The escape character is the normal C backslash and
  64.  *    the normal C escape sequences are recognized, along
  65.  *    with a special sequence for the ASCII escape character
  66.  *    (\033).  The recognized sequences are:
  67.  *
  68.  *        \E   =>  '\033'  (ASCII escape character)
  69.  *        \b   =>  '\010'  (ASCII backspace character)
  70.  *        \f   =>  '\014'  (ASCII form feed character)
  71.  *        \n   =>  '\012'  (ASCII newline/linefeed char)
  72.  *        \r   =>  '\015'  (ASCII carriage return char)
  73.  *        \t   =>  '\011'  (ASCII tab character)
  74.  *        \ddd =>  '\ddd'  (arbitrary ASCII digit)
  75.  *        \x   =>  'x'     (ordinary ASCII character)
  76.  *
  77.  */
  78.  
  79. #include <stdio.h>
  80.  
  81. extern char *_tcpbuf;        /* Termcap entry buffer pointer */
  82.  
  83. /*
  84.  *  PSEUDO CODE
  85.  *
  86.  *    Begin tgetstr
  87.  *        Initialize pointer to the termcap entry buffer.
  88.  *        While there is a field to process
  89.  *        Skip over the field separator character.
  90.  *        If this is the entry we want then
  91.  *            If the entry is not a string then
  92.  *            Return NULL.
  93.  *            Else
  94.  *            Transfer string and rtn pointer.
  95.  *            End if
  96.  *        End if
  97.  *        End while
  98.  *        Return NULL
  99.  *    End tgetstr
  100.  *
  101.  */
  102.  
  103. char *tgetstr(id,area)
  104. char *id;
  105. char **area;
  106. {
  107.     char *bp;
  108.     extern char *index();
  109.     char *decode();
  110.  
  111.     bp = _tcpbuf;
  112.     while ((bp = index(bp,':')) != NULL) {
  113.     bp++;
  114.     if (*bp++ == id[0] && *bp != NULL && *bp++ == id[1]) {
  115.         if (*bp != NULL && *bp++ != '=') {
  116.         return(NULL);
  117.         } else {
  118.         return(decode(bp,area));
  119.         }
  120.     }
  121.     }
  122.     return(NULL);
  123. }
  124.  
  125. /*
  126.  *  INTERNAL FUNCTION
  127.  *
  128.  *    decode   transfer string capability, decoding escapes
  129.  *
  130.  *  SYNOPSIS
  131.  *
  132.  *    static char *decode(bp,area)
  133.  *    char *bp;
  134.  *    char **area;
  135.  *
  136.  *  DESCRIPTION
  137.  *
  138.  *    Transfers the string capability, up to the next ':'
  139.  *    character, or null, to the buffer pointed to by
  140.  *    the pointer in *area.  Note that the initial
  141.  *    value of *area and *area is updated to point
  142.  *    to the next available location after the null
  143.  *    terminating the transfered string.
  144.  *
  145.  *  BUGS
  146.  *
  147.  *    There is no overflow checking done on the destination
  148.  *    buffer, so it better be large enough to hold
  149.  *    all expected strings.
  150.  *
  151.  */
  152.  
  153. /*
  154.  *  PSEUDO CODE
  155.  *
  156.  *    Begin decode
  157.  *        Initialize the transfer pointer.
  158.  *        While there is an input character left to process
  159.  *        Switch on input character
  160.  *        Case ESCAPE:
  161.  *            Decode and xfer the escaped sequence.
  162.  *            Break
  163.  *        Case CONTROLIFY:
  164.  *            Controlify and xfer the next character.
  165.  *            Advance the buffer pointer.
  166.  *            Break
  167.  *        Default:
  168.  *            Xfer a normal character.
  169.  *        End switch
  170.  *        End while
  171.  *        Null terminate the output string.
  172.  *        Remember where the output string starts.
  173.  *        Update the output buffer pointer.
  174.  *        Return pointer to the output string.
  175.  *    End decode
  176.  *
  177.  */
  178.  
  179. static char *decode(bp,area)
  180. char *bp;
  181. char **area;
  182. {
  183.     char *cp, *bgn;
  184.     char *do_esc();
  185.  
  186.     cp = *area;
  187.     while (*bp != NULL && *bp != ':') {
  188.     switch(*bp) {
  189.     case '\\':
  190.         bp = do_esc(cp++,++bp);
  191.         break;
  192.     case '^':
  193.         *cp++ = *++bp & 037;
  194.         bp++;
  195.         break;
  196.     default:
  197.         *cp++ = *bp++;
  198.         break;
  199.     }
  200.     }
  201.     *cp++ = NULL;
  202.     bgn = *area;
  203.     *area = cp;
  204.     return(bgn);
  205. }
  206.  
  207. /*
  208.  *  INTERNAL FUNCTION
  209.  *
  210.  *    do_esc    process an escaped sequence
  211.  *
  212.  *  SYNOPSIS
  213.  *
  214.  *    char *do_esc(out,in);
  215.  *    char *out;
  216.  *    char *in;
  217.  *
  218.  *  DESCRIPTION
  219.  *
  220.  *    Processes an escape sequence pointed to by
  221.  *    in, transfering it to location pointed to
  222.  *    by out, and updating the pointer to in.
  223.  *
  224.  */
  225.  
  226. /*
  227.  *  PSEUDO CODE
  228.  *
  229.  *    Begin do_esc
  230.  *        If the first character is not a NULL then
  231.  *        If is a digit then
  232.  *            Set value to zero.
  233.  *            For up to 3 digits
  234.  *                Accumulate the sum.
  235.  *            End for
  236.  *            Transfer the sum.
  237.  *            Else if character is in remap list then
  238.  *            Transfer the remapped character.
  239.  *            Advance the input pointer once.
  240.  *            Else
  241.  *            Simply transfer the character.
  242.  *            End if
  243.  *        End if
  244.  *        Return updated input pointer.
  245.  *    End do_esc
  246.  *
  247.  */
  248.  
  249. static char *maplist = {
  250.     "E\033b\bf\fn\nr\rt\t"
  251. };
  252.  
  253. char *do_esc(out,in)
  254. char *out;
  255. char *in;
  256. {
  257.     int count;
  258.     char ch;
  259.     char *cp;
  260.     extern int isdigit();
  261.  
  262.     if (*in != NULL) {
  263.     if (isdigit(*in)) {
  264.         ch = 0;
  265.         for (count = 0; count < 3 && isdigit(*in); in++) {
  266.          ch <<= 3;
  267.          ch |= (*in - '0');
  268.         }
  269.         *out++ = ch;
  270.     } else if ((cp = index(maplist,*in)) != NULL) {
  271.         *out++ = *++cp;
  272.         in++;
  273.     } else {
  274.         *out++ = *in++;
  275.     }
  276.     }
  277.     return(in);
  278. }
  279.